package com.gamesnet.ihm;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.gamesnet.api.Api;
import com.gamesnet.models.Message;
import com.gamesnet.widget.ActionBar;
import com.gamesnet.widget.ActionBar.IntentAction;

public class DetailMessageEnvoyeActivity extends Activity{
	
	@Override
	public void onCreate(Bundle savedInstanceState) { 

		super.onCreate(savedInstanceState); 
		setContentView(R.layout.message);
		
		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbarHaut);
		actionBar.setHomeAction(new IntentAction(this, new Intent(this, DashboardActivity.class), R.drawable.home));
		actionBar.setTitre(PageTitle.MESSAGE);
		
		Bundle b = getIntent().getExtras();
		long messageId = b.getLong("messageId");
		long userId = b.getLong("destinataireId");
		
		try {
			Message message = Api.messageEnvoye(String.valueOf(messageId), String.valueOf(userId));
			ImageView imageMail = (ImageView)findViewById(R.id.pictureMail);
			URL urlImage = new URL("http://10.0.2.2:8080/Gamesnet/images/upload/resized/avatar/"+message.getDestinataires().image()+".jpg");
			HttpURLConnection connection = (HttpURLConnection) urlImage.openConnection();
			InputStream inputStream = connection.getInputStream();
			Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
			imageMail.setImageBitmap(bitmap);
			
			TextView exp = (TextView)findViewById(R.id.mailUser);
			exp.setText(message.getDestinataires().afficher());
			
			TextView object = (TextView)findViewById(R.id.mailObject);
			object.setText(message.getObjet());
			
			TextView content = (TextView)findViewById(R.id.mailContent);
			content.setText(message.getContenu());
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
